package com.hr;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.espirit.eap.manager.Department;
import com.espirit.eap.manager.DepartmentService;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.util.StatGenericJPAService;
import com.espirit.finance.core.AcctTerm;
import com.googlecode.cswish.annotation.ParamScope;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.ParameterMap;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.spring.SearchCallback;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class SalaryDefineService {
	
	@Resource
	StatGenericJPAService genericService;
	
	@Resource
	DepartmentService departmentService;
	
	public  PageInfo search(final ParameterMap model, int start, int limit,
    		String sort, String dir, boolean newSearch, Reference<QLInfo> qlInfoRef,
    		@ParamScope(visible=false, value="#ps")PageElements ps ,boolean fromMenu
    		) {
		
		SearchCallback callback = departmentService.buildDeptDefaultSearchCallback(model, null, null);
		return genericService.search(model, start, limit, sort, dir, newSearch, qlInfoRef, callback, true, false);
	}
	
	public void add(SalaryDefine model){
		checkDeptIds(model);
		genericService.add(model);
	}
	
	private void checkDeptIds(SalaryDefine model){
		String deptIds = model.getDeptIds();
		if (deptIds != null) {
			int pos = deptIds.indexOf(",");
			if (pos > 0){
				throw new ApplicationException("只准选择一个部门");
			} else {
				model.setDepartment(new Department(deptIds));
			}
		}
	}
	
	public void _update(SalaryDefine model){
		model = genericService.view(model);
		if (model.getDepartment() != null){
			String deptIds = String.valueOf(model.getDepartment().getId());
			model.setDeptIds(deptIds);
		}
	}
	
	public void update(SalaryDefine model, @ParamScope(visible=false, value="#ps")PageElements ps){
		checkDeptIds(model);
		genericService.update(model);
	}
	
	public SalaryDefine getSalaryDefine(String empId, AcctTerm acctTerm){
		if (empId == null) return null;
		
		Employee employee = genericService.load(Employee.class, empId);
		
		if (employee == null || employee.getDepartment() == null) return null;
		
		SalaryDefine salaryDefine = getSalaryDefineByDeptId(employee.getDepartment().getId(), acctTerm);
		return salaryDefine;
	}
	
	public SalaryEmployeeDefine getSalaryEmployeeDefine(String empId, AcctTerm acctTerm){
		SalaryDefine salaryDefine = getSalaryDefine(empId, acctTerm);
		if (salaryDefine != null){
			SalaryEmployeeDefine example = null;
			List<SalaryEmployeeDefine> employeeDefines = salaryDefine.getEmployeeDefines();
			if (employeeDefines != null){
				for(SalaryEmployeeDefine salaryEmployeeDefine : employeeDefines){
					if (salaryEmployeeDefine.getEmployee().getId().equals(empId)){
						example = salaryEmployeeDefine;
						break;
					}
				}
				
				if (example == null){
					example = new SalaryEmployeeDefine();
					example.setDaySalaryHours(salaryDefine.getDaySalaryHours());
					example.setMonthSalaryDays(salaryDefine.getMonthSalaryDays());
				}
				
				return example;
			}
			
		}
		return null;
	}
	
	//TODO: cach it
	public SalaryDefine getSalaryDefineByDeptId(String deptId, AcctTerm acctTerm){
		if (deptId == null) return null;
		
		SalaryDefine example = new SalaryDefine();
		ModelUtil.setProperty(example, "department.id", deptId);
		example.setAcctTerm(acctTerm);
		
		example = genericService.searchAll(example).getData(0);
		if (example != null){
			return example;
		} else {
			Department parent = departmentService.getParentDepartment(deptId);
			if (parent != null){
				return getSalaryDefineByDeptId(parent.getId(), acctTerm);
			}
		}
		return null;
	}
	
}
